package com.tong.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tong.entity.Article;
import com.tong.entity.vo.ArticleQuery;
import com.tong.mapper.ArticleMapper;
import com.tong.service.ArticleService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * @author tong
 * @since 2021-09-29
 */
@Service
public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, Article> implements ArticleService {


    @Override
    public void listArticle(Page<Article> articlePage, ArticleQuery articleQuery) {
        QueryWrapper<Article> articleWrapper = new QueryWrapper<>();
        articleWrapper.orderByDesc("update_time");
        //因为首页并不需要显示内容，所以需要排除掉内容字段
        articleWrapper.select(Article.class, i -> !i.getProperty().equals("content"));
        if (articleQuery == null) {
            baseMapper.selectPage(articlePage, articleWrapper);
            return;
        }
        String content = articleQuery.getContent();
        Integer tagId = articleQuery.getTagId();
        Integer typeId = articleQuery.getTypeId();

        if (!StringUtils.isEmpty(content)) {
            articleWrapper.like("title", content)
                    .or().like("summary", content)
                    .or().like("content", content);
        }
        if (tagId != null) {
            articleWrapper.eq("tag_id", tagId);
        }
        if (typeId != null) {
            articleWrapper.eq("type_id", typeId);
        }
        baseMapper.selectPage(articlePage, articleWrapper);
    }
}
